package org.ktjiaoyu;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.ktjiaoyu.mapper.PermissionMapper;
import org.ktjiaoyu.mapper.RoleMapper;
import org.ktjiaoyu.mapper.UserMapper;
import org.ktjiaoyu.pojo.*;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jane
 * @date 2025-03-11 11:44
 */
@SpringBootTest
public class JDBCTest373 {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    @Test
    public void permissionTest(){
        //获取角色
        String username ="admin";
        LambdaQueryWrapper<SysUser> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername,username);

        SysUser sysUser = userMapper.selectOne(queryWrapper);

        //根据用户查询角色集合
        List<Role> roleList  = roleMapper.selectJoinList(Role.class,
                            new MPJLambdaWrapper<Role>()
                                    .selectAll(Role.class)
                                    .innerJoin(UserRole.class,UserRole::getRoleId,Role::getId)
                                   // .innerJoin(SysUser.class,SysUser::getId,UserRole::getUserId)
                                    .eq(UserRole::getUserId,sysUser.getId()));

        //转成[]
        List<Integer> collect = roleList.stream().map(Role::getId).collect(Collectors.toList());

        System.out.println("collect: "+ collect);

        //根据用户角色查询权限集合
        List<Permission> permissions =permissionMapper.selectJoinList(Permission.class,
                            new MPJLambdaWrapper<Permission>()
                                    .selectAll(Permission.class)
                                    .innerJoin(RolePermission.class,RolePermission::getPermission,Permission::getId)
                                    .innerJoin(Role.class,Role::getId,RolePermission::getRoleId)
                                    .in(Role::getId,collect.toArray())
            );


        System.out.println(permissions);
        //处理权限列表 [ ]
        List<String> strList =permissions.stream().map(Permission::getPermit).collect(Collectors.toList());
        System.out.println(strList);
    }
}
